package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @program: jxc
 * @description: 商品實現接口
 * @author: SXC
 * @create: 2021-09-04 10:12
 **/
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    /**
     * 获取数据
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @Override
    public List<Goods> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        page  = page < 1 ? 0 : (page -1)*rows;
        rows  = rows < 1 ? 10 : rows;
        return goodsDao.selectListInventory(page, rows, codeOrName, goodsTypeId);

    }

    /**
     * 获取条数
     * @return
     */
    @Override
    public int count() {
        return  goodsDao.selectCount();
    }

    /**
     * 获取数据
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @Override
    public List<Goods> goodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        page  = page < 1 ? 0 : (page -1)*rows;
        rows  = rows < 1 ? 10 : rows;
        return goodsDao.selectGoodsList(page, rows, goodsName, goodsTypeId);
    }

    @Override
    public ServiceVO getCode() {
        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    /**
     * 商品添加或修改
     * @param goods
     */
    @Override
    public void save(Goods goods) {
        if (goods != null){
            if(goods.getGoodsId() == null){
                //添加商品没有库存
                goods.setInventoryQuantity(0);
                //表示初始值
                goods.setState(0);
                goodsDao.save(goods);
            }else {
                goodsDao.update(goods);
            }
        }

    }

    /**
     * 商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
     * @param goodsId
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer goodsId) {
        Goods goods =  goodsDao.selectGoods(goodsId);
        if (goods != null){
            if ( goods.getState() == 0){
                goodsDao.delete(goodsId);
            }
        }
    }

    /**
     * 获取无库存数据
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page  = page < 1 ? 0 : (page -1)*rows;
        rows  = rows < 1 ? 10 : rows;
        return goodsDao.getNoInventoryQuantity(page,rows,nameOrCode);
    }

    /**
     * 获取无库存条数
     * @return
     */
    @Override
    public int countNoInventoryQuantity() {
        return goodsDao.countNoInventoryQuantity();
    }

    /**
     * 获取所有库存数据
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page  = page < 1 ? 0 : (page -1)*rows;
        rows  = rows < 1 ? 10 : rows;
        return goodsDao.getHasInventoryQuantity(page,rows,nameOrCode);    }

    /**
     * 获取有库存条数
     * @return
     */
    @Override
    public int countHasInventoryQuantity() {
        return goodsDao.countHasInventoryQuantity();
    }

    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     */
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        if (goodsId != null){
            //修改
            goodsDao.saveStock(goodsId,inventoryQuantity,null);
        }else {
            //添加
            goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        }
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     */
    @Override
    public void deleteStock(Integer goodsId) {
        Goods goods = goodsDao.selectGoods(goodsId);
        if (goods != null){
            if (goods.getState() == 0){
                goodsDao.delete(goodsId);
            }
        }
    }

    /**
     * 3、库存报警
     *  * 3.1、查询所有 当前库存量 小于 库存下限的商品信息
     * @return
     */
    @Override
    public List<Goods> listAlarm() {
        return goodsDao.selectListAlarm();
    }
}
